package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void add(SetmealDTO setmealDTO) {
        //此处要操作两张表  setmeal setmeal_dish
        //属性拷贝 setmeal
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //插入数据
        setmealMapper.insert(setmeal);

        /*要想获取插入后的id值，sql语句必须写到xml文件中 并且指定两个特有属性*/
        Long id = setmeal.getId();


        //对setmeal_dish进行操作
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
        }
        setmealDishMapper.insert(setmealDishes);
    }

    /**
     * 分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //设置分页参数
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        /*
         *当进行多表查询时，如果碰到查询出来的名字和属性名不一致，就可以取别名，mybatis任然能够自动识别
         * （只要名称一样就可以自动识别，别名也可以）
         *就查询出来的数据多余所封装的对象也没事，mybatis自动识别*/

        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void update(Integer status, Long id) {
        //启售停售逻辑
        //当前套餐关联了未启售的菜品、无法启售
        //1、查询到当前套餐关联的菜品
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
        //2、当前菜品有没有未启售
        for (SetmealDish setmealDish : setmealDishes) {
            Long dishId = setmealDish.getDishId();
            Dish dish = dishMapper.getById(dishId);
            if (dish.getStatus().equals(StatusConstant.DISABLE)) {
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        Setmeal setmeal = setmealMapper.getById(id);
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);
    }

    @Override
    public SetmealVO getById(Long id) {
        //查两张表 setmeal setmeal_dish
        SetmealVO setmealVO = new SetmealVO();

        //属性拷贝
        Setmeal setmeal = setmealMapper.getById(id);
        BeanUtils.copyProperties(setmeal, setmealVO);

        //查setmeal_dish表
        List<SetmealDish> setmealDishList = setmealDishMapper.getBySetmealId(id);

        setmealVO.setSetmealDishes(setmealDishList);

        /*
         * 注意：虽然setmealVO中的categoryName为null，但是不影响数据回显
         * 但是可以做的更好*/
        //在这里可以为categoryName赋值
        /*
         * 在前几个接口中也可以这样做，就是单独查找categoryName*/
        String categoryName = setmealMapper.getCategoryNameById(id);

        setmealVO.setCategoryName(categoryName);
        return setmealVO;


    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        //修改setmeal表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

        //套餐id
        Long id = setmealDTO.getId();
        //修改setmeal_dish表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //把当前掏槽对应的setmeal_dish表中的数据删除 条件 id=setmeal_id
        setmealDishMapper.deleteBySetmealId(id);

        //再将新增加的setmealDishes插入

        if (setmealDishes != null && setmealDishes.size() > 0) {
            //setmeal可能为null，先赋值
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(id);
            }
            //插入多条数据
            setmealDishMapper.insert(setmealDishes);
        }

    }

    @Override
    public void deleteByIds(List<Long> ids) {
        //删除逻辑
        //1、启售的套餐不可删除

        //判断套餐可不可以删除
        //1、启售的套餐不可删除
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if (setmeal.getStatus().equals(StatusConstant.ENABLE)) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        setmealMapper.deleteByIds(ids);

    }

    @Override
    public List<Setmeal> getByCategoryId(Long categoryId) {
        //只查询在售的套餐
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(StatusConstant.ENABLE);
        setmeal.setCategoryId(categoryId);
        return setmealMapper.getBySetmeal(setmeal);
    }

    //根据套餐id查询包含的菜品
    @Override
    public List<DishItemVO> getBySetmealId(Long id) {
        List<DishItemVO> list = new ArrayList<>();
        //setmeal_dish表
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
        //DishItemVO 有四个字段 copies  name 在setmeal_dish    image description在setmeal中需要用dish_id来查
        for (SetmealDish setmealDish : setmealDishes) {
            DishItemVO dishItemVO = new DishItemVO();

            dishItemVO.setCopies(setmealDish.getCopies());
            dishItemVO.setName(setmealDish.getName());

            Dish dish = dishMapper.getById(setmealDish.getDishId());
            dishItemVO.setDescription(dish.getDescription());
            dishItemVO.setImage(dish.getImage());
            list.add(dishItemVO);
        }

        return list;
    }
}
